/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.nyquistmeteocal.beans;

import it.nyquistmeteocal.boundary.EventManager;
import it.nyquistmeteocal.control.OurEvent;
import it.nyquistmeteocal.entity.Event;
import java.awt.event.ActionEvent;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import org.primefaces.event.ScheduleEntryMoveEvent;
import org.primefaces.event.ScheduleEntryResizeEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleEvent;
import org.primefaces.model.ScheduleModel;

/**
 *
 * @author stefano
 */
@ManagedBean
@ViewScoped
public class ScheduleBean implements Serializable {

    @EJB
    private EventManager em;

    private ScheduleModel eventModel;
    private ScheduleEvent event = new DefaultScheduleEvent();
    private List<Event> cutEvents = new ArrayList<Event>();
    private Integer i;
    private Integer count = 0;
    private OurEvent ourevent = new OurEvent();

    public ScheduleBean() {
    }

    public ScheduleModel getEventModel() {
        return eventModel;
    }

    public void setEventModel(ScheduleModel eventModel) {
        this.eventModel = eventModel;
    }

    public ScheduleEvent getEvent() {
        return event;
    }

    public void setEvent(ScheduleEvent event) {
        this.event = event;
    }

    public List<Event> getCutEvents() {
        return cutEvents;
    }

    public void setCutEvents(List<Event> cutEvents) {
        this.cutEvents = cutEvents;
    }

    @PostConstruct
    public void init() {
        eventModel = new DefaultScheduleModel();

        //eventModel.addEvent(new DefaultScheduleEvent("Champions League Match", previousDay8Pm(), previousDay11Pm()));
        //eventModel.addEvent(new DefaultScheduleEvent("Birthday Party", today1Pm(), today6Pm()));
        //eventModel.addEvent(new DefaultScheduleEvent("Breakfast at Tiffanys", nextDay9Am(), nextDay11Am()));
        //eventModel.addEvent(new DefaultScheduleEvent("Plant the new garden stuff", theDayAfter3Pm(), fourDaysLater3pm()));
        cutEvents = em.getEvents();
        Integer count = cutEvents.size();
        System.out.println("The size is: " + count);

        for (i = 0; i < count; i++) {
            Event currentEvent = cutEvents.get(i);
            eventModel.addEvent(new DefaultScheduleEvent(currentEvent.getEventname(), startTime(currentEvent), finishTime(currentEvent)));
        }

        //eventModel.addEvent(new DefaultScheduleEvent(cutEvents.getEventname(i), start(), finish()));
    }

    private Date startTime(Event event) {
        Calendar t = (Calendar) today().clone();
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
        Date d1 = null;
        try {
            d1 = format.parse(event.getEventdate());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        t.setTime(d1);
        t.set(Calendar.HOUR, event.getTimestart());
        return t.getTime();
    }

    private Date finishTime(Event event) {
        Calendar t = (Calendar) today().clone();
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
        Date d1 = null;
        try {
            d1 = format.parse(event.getEventdate());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        t.setTime(d1);
        t.set(Calendar.HOUR, event.getTimefinish()); ///here the +1 without the GMT+1
        return t.getTime();
    }

    private Calendar today() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), 0, 0, 0);

        return calendar;
    }

    public void addEvent(ActionEvent actionEvent) {
        if (event.getId() == null) {
            eventModel.addEvent(event);
        } else {
            eventModel.updateEvent(event);
        }

        event = new DefaultScheduleEvent();
    }

    public void onEventSelect(SelectEvent selectEvent) {
        event = (ScheduleEvent) selectEvent.getObject();
    }

    public void onDateSelect(SelectEvent selectEvent) {
        event = new DefaultScheduleEvent("", (Date) selectEvent.getObject(), (Date) selectEvent.getObject());
    }

    public void onEventMove(ScheduleEntryMoveEvent event) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Event moved", "Day delta:" + event.getDayDelta() + ", Minute delta:" + event.getMinuteDelta());

        addMessage(message);
    }

    public void onEventResize(ScheduleEntryResizeEvent event) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Event resized", "Day delta:" + event.getDayDelta() + ", Minute delta:" + event.getMinuteDelta());

        addMessage(message);
    }

    private void addMessage(FacesMessage message) {
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public Date getInitialDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), Calendar.FEBRUARY, calendar.get(Calendar.DATE), 0, 0, 0);

        return calendar.getTime();
    }
}
